package acm.pta9;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;

public class Main {
    public static boolean isPrime(int n) {
        if (n < 3) return n > 1;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }

    private static Set<Integer> iterateSet = new HashSet<>();
    private static Map<Integer, Integer> map = new HashMap<>();

    public static void check(int a, int b) {
        //这是什么语法
        label:
        for (int i = a; i <= b; i++) {
            Set<Integer> set = new HashSet<>();
            int iterate = i;
            while (iterate != 1) {
                if (!set.add(iterate)) {
                    continue label;
                }
                int x = iterate;
                int sumOfSquare = 0;
                while (x != 0) {
                    sumOfSquare += (int) Math.pow(x % 10, 2);
                    x /= 10;
                }
                iterate = sumOfSquare;
                iterateSet.add(iterate);
            }
            map.put(i, set.size());
        }
    }

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int a = (int) in.nval;
        in.nextToken();
        int b = (int) in.nval;
        check(a, b);
        StringBuilder sb = new StringBuilder();
        List<Integer> list = new ArrayList<>(map.keySet());
        list.removeAll(iterateSet);
        list.sort(null);
        for (Integer value : list) {
            int x = map.get(value);
            sb.append(value).append(" ").append(isPrime(value) ? 2 * x : x).append("\n");
        }
        System.out.print(sb.length() > 0 ? sb : "SAD");

    }
}
